/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.informet.ipd.modelo.dao;

import com.informet.ipd.struts.form.BeanMain;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Vector;
import javax.swing.JOptionPane;

/**
 *
 * @author LAPTOP01
 */
public class MainDao extends DBase {

    Connection conn = null;
    Object restrinnciones;

     public MainDao() {
        super();
    }

     public List consulta(String sql) {

        List<Object[]> list = new ArrayList<Object[]>();
        try {
            conn = DriverManager.getConnection(url, usuario, password);

            if (conn == null) {
                list = null;
            } else {
                Statement st = conn.createStatement();
                ResultSet rs = st.executeQuery(sql);
                ResultSetMetaData rm = rs.getMetaData();
                int numCols = rm.getColumnCount();


                while (rs.next()) {
                    Object[] fila = new Object[numCols];
                    for (int i = 0; i < numCols; i++) {
                        fila[i] = rs.getObject(i + 1);
                    }
                    list.add(fila);
                }
                conn.close();
            }
        } catch (SQLException e) {
            list = null;
            JOptionPane.showMessageDialog(null, e.toString());
        }
//JOptionPane.showMessageDialog(null, list.get(0));
        return list;    // returna null si falla
    }

     public StringBuffer menu() {
        StringBuffer menu = new StringBuffer("");

        try {


            List list = consulta("select nombre ,url,menu_idref,menu_id from menu");

            int head = 0;
            if (list != null) {

                Object[] fila = (Object[]) list.get(0);


                for (int fil = 0; fil < list.size(); ++fil) {

                    fila = (Object[]) list.get(fil);

                    if (fila[1].equals("vacio")) {
                        menu.append("<li>");
                    }

                    for (int col = 0; col < fila.length; ++col) {

                        Object campo = fila[col];

                        if (campo.equals(0)) {
                            menu.append("<a href='#' class='parent'><span>" + fila[0] + "</span></a>");

                            Object[] fila2 = (Object[]) list.get(0);
                               menu.append("<ul> ");
                            for (int fil2 = 0; fil2 < list.size(); ++fil2) {
                                     fila2 = (Object[]) list.get(fil2);
//                                for (int col2 = 0; col2 < fila2.length; ++col2) {
                                    //                          JOptionPane.showMessageDialog(null, fila2.length);
//                                    JOptionPane.showMessageDialog(null, fila2[2] + " & " + fila[3]);

                                    if (fila2[2].equals(fila[3])) {
                                       
                                        menu.append("<li> ");
                                        menu.append("<a href='#'  onclick=\"fu_url('"+fila2[1]+"')\" ><span>" + fila2[0] + "</span></a>");
                                        menu.append(" </li>");
                                       
                                    }

//                                }

                            }
                                menu.append(" </ul> ");

                        }
                    }



                    if (fila[1].equals("vacio")) {
                        menu.append("</li>");
                    }
                }

//                menu.append("<a href='#' class='parent'><span>" + fila[0] + "</span></a>");


            }

            return menu;


        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.toString());

            return menu;
        }

    }


     public  Object getFila(String sql) {
        List list = consulta(sql);
        Object fila = null;

     
            if (list.size() >= 1) {
                fila =  list.get(0);
            }
        return fila;
    }

     public Object getCampo(String sql) {
     Object campo ="";
     try{

        Object fila = getFila(sql);


            campo = fila;

            if(campo==""){
                campo="vacio";
            }
       


     }catch(Exception e){
         JOptionPane.showMessageDialog(null, e.toString());
     }
        
        return campo;
    }

      public Object validacion(int id){
ResourceBundle bundle = ResourceBundle.getBundle("com.informet.ipd.modelo.dao.database");
      Object name="";
     try{
         name= getCampo(bundle.getString("validacion").trim()+id);
         
     }catch(Exception e){
         JOptionPane.showMessageDialog(null, e.toString());
     }

     return name;
 }

 


}


